<?php

declare(strict_types=1);

namespace App\ChannelCode\Controller\Dashboard;

use App\ChannelCode\Logic\IndexLogic;
use App\ChannelCode\Logic\StoreLogic;
use Base\Controller\AbstractController;
use Base\Constants\ErrorCode;
use Base\Exception\CommonException;
use Base\Request\ValidateSceneTrait;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Common\Middleware\DashboardAuthMiddleware;

/**
 * 渠道码列表.
 *
 * Class IndexController
 * @Controller
 */
class IndexController extends AbstractController
{
    //验证类
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var IndexLogic
     */
    private $indexLogic;

    /**
     * @param IndexLogic $corpService
     * @param RequestInterface $request
     */
    public function __construct(IndexLogic $corpService,RequestInterface $request)
    {
        
        $this->indexLogic = $this->indexLogic??$corpService;
        $this->request = $this->request??$request;
    }

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/channelCode/index", methods="get")
     */
    public function index()
    {
        //接收参数
        $params = $this->request->inputs(
            [
                'name', 'type', 'groupId', 'current', 'size',
            ],
            [
                'current' => 1, 'size' => 20,
            ]
        );
        $params['corpId'] = $this->request->input('userInfo.corp_id');
        $params['tenantId'] = $this->request->input('userInfo.tenant_id');
        $params['userId'] = $this->request->input('userInfo.user_id');
        $params['deptId'] = $this->request->input('userInfo.dept_id');
        $data = $this->indexLogic->getListPage($params);
        return $data??[];
    }

    /**
     * 创建渠道码
     * @Inject
     * @var StoreLogic
     */
    protected $storeLogic;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/channelCode/store", methods="POST")
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @return array
     */
    public function handle()
    {
        $corpId = $this->request->input('userInfo.corp_id');
        $tenantId = $this->request->input('userInfo.tenant_id');
        $userId = $this->request->input('userInfo.user_id');
        $deptId = $this->request->input('userInfo.dept_id');
        if (!isset($corpId)) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '请先选择企业');
        }

        //接收参数
        $params['baseInfo'] = $this->request->input('baseInfo');
        $params['drainageEmployee'] = $this->request->input('drainageEmployee');
        $params['welcomeMessage'] = $this->request->input('welcomeMessage');
        $params['corpId'] = $corpId;
        $params['tenantId'] = $tenantId;
        $params['userId'] = $userId;
        $params['deptId'] = $deptId;
        return $this->storeLogic->handle($params);
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [];
    }

}
